<?php
/**
 * Created by PhpStorm.
 * User: su
 * Date: 2017/12/28
 * Time: 16:03
 */

namespace app\index\controller;

use app\index\model\Link;
use app\index\model\Users;
use app\index\model\Logis;
use think\Controller;
use think\response\Redirect;
use think\view;


class Admin extends Controller
{
    public function login()
    {
        return $this->fetch();

    }

    public function index()
    {
        $user_id = session('user_id');

        if (!$user_id) {
            $this->error('请登录', '/index/Admin/login');
        }

        $link_obj = new Link();
        $link_info = $link_obj->getLinkData();

        $this->assign('link_info', $link_info);
        $this->assign('title', '链接展示');
        $this->assign('links', 'index');
        return $this->fetch();
    }

    public function logs(){
        $user_id = session('user_id');

        if (!$user_id) {
            $this->error('请登录', '/index/Admin/login');
        }

        $logs_obj = new Logis();
        $logs_info = $logs_obj->getDataList();
        $this->assign('logs_info', $logs_info);
        $this->assign('title', '查看量');
        $this->assign('links', 'logs');
        return $this->fetch();
    }

    public function addlink()
    {

        $user_id = session('user_id');

        if (!$user_id) {
            $this->error('请登录', '/index/Admin/login');
        }

        if(\request()->isAjax() || \request()->isPost()){

            $ios_link = input('ios_link');
            $android_link = input('android_link');

            if (empty($ios_link)) {
                $this->error('链接为空');
            }
            if (empty($android_link)) {
                $this->error('链接为空');
            }

            $arr = array(
                'link_href_ios'  => $ios_link,
                'link_href_andorer'  => $android_link
            );


            $link_obj = new Link();
            $r = $link_obj->addLink($arr);

            if ($r) {
                $this->success('添加成功', '/index/Admin/index');
            } else {
                $this->error('添加错误');

            }
        }

        $this->assign('title', '添加链接');
        $this->assign('links', 'addlink');
        return $this->fetch();

    }

    public function logining()
    {
        $name = input('username');
        $password = input('password');

        $user_obj = new Users();

        $data = array(
            'uname' => $name,
            'password' => $password,
        );
        $r = $user_obj->getUsers($data);

        if ($r) {
            $this->success('登录成功', '/index/Admin/index');

        } else {
            return $this->error('用户名错误或密码错误');
        }
    }

    public function delete_link(){
        
        $user_id = session('user_id');
       
        if (!$user_id) {
            $this->error('请登录', '/index/Admin/login');
        }

        $link_id = input('id');
        $link_obj = new Link();
        if($link_obj->setdeleteLink($link_id,array('is_deletes' => '1'))){

            $this->success('删除成功');

        }else{
            return $this->error('删除失败');
        }
    }


    public function updata(){
        
        $user_id = session('user_id');
       
        if (!$user_id) {
            $this->error('请登录', '/index/Admin/login');
        }

        $link_id = input('id');
        $link_obj = new Link();

        if(\request()->isAjax() || \request()->isPost()){
            $ios_link = input('ios_link','string');
            $android_link  = input('android_link','string');

            if (empty($ios_link)) {
                $ios_link = 'https://itunes.apple.com/app/apple-store/id1313146736?pt=118874591&ct=tuiguan1&mt=8';
            }
            if (empty($android_link)) {
                $this->error('链接为空');
            }


            $arr = array(
                'link_href_ios'  => $ios_link,
                'link_href_andorer'  => $android_link
            );
            if($link_obj->setdeleteLink($link_id,$arr)){

                return $this->success('修改成功');
    
            }else{
                return $this->error('修改失败');
            }
        }

        $link_info = $link_obj->getLinkUser($link_id);


        $this->assign('title', '修改链接');
        $this->assign('links', 'updata');
        $this->assign('link_info',$link_info);
        return $this->fetch();
    }

}
